package com.xckj.zuoliao.presenter;

import androidx.annotation.NonNull;

import com.xckj.zuoliao.bean.DetailsBean;
import com.xckj.zuoliao.framework.BaseBean;
import com.xckj.zuoliao.framework.net.RetrofitManager;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.IDetailsView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: 代码的搬运工
 * @Describe: 详情
 * @Date: 2024/3/31 22:24
 */
public class DetailsPresenter {
    private final IDetailsView detailsView;

    public DetailsPresenter(IDetailsView detailsView) {
        this.detailsView = detailsView;
    }

    /**
     * @param area_type 地区 1:澳门 2:香港
     * @param colum     活动
     * @param stage_no  期数
     */
    public void getDetails(String uid, String area_type, String colum, String stage_no) {
        detailsView.showLoading();
        Call<BaseBean<DetailsBean>> call = RetrofitManager.getInstance().getService().getDetails(uid, area_type, colum, stage_no);
        call.enqueue(new Callback<BaseBean<DetailsBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<DetailsBean>> call, @NonNull Response<BaseBean<DetailsBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<DetailsBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        detailsView.success(baseBean.data);
                    } else {
                        detailsView.failed(baseBean.msg);
                    }
                } else {
                    detailsView.failed("请求失败");
                }
                detailsView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<DetailsBean>> call, @NonNull Throwable t) {
                t.printStackTrace();
                detailsView.failed("请求失败");
            }
        });
    }
}
